# -*- coding: utf-8 -*-

from rest_framework.decorators import action
from core.viewset import CustomModelViewSet
from core.response import JsonResponse
from apps.sys.models import Dept
from apps.sys.dept.serializers import *
from utils.tree import list2tree
from .filters import *


class DeptViewSet(CustomModelViewSet):
    """
    部门管理接口
    list:查询
    create:新增
    update:修改
    retrieve:单例
    destroy:删除
    """

    queryset = Dept.objects.all()
    serializer_class = DeptSerializer
    create_serializer_class = DeptCreateUpdateSerializer
    update_serializer_class = DeptCreateUpdateSerializer
    filterset_class = DeptFilterSet

    @action(detail=False, methods=["GET"])
    def treeselect(self, request, *args, **kwargs):
        queryset = Dept.objects.all()
        serializer = DeptTreeSelectSerializer(queryset, many=True, request=request)
        data = list2tree(serializer.data)
        return JsonResponse(data=data, msg="获取成功")
